ExcelApplication Class

Used to automate Microsoft Excel.

Events

None

Properties

None

Methods

None

More information available in parent classes: OLEObject:Object


Notes

The language that you use to automate Microsoft Office applications is documented by Microsoft and numerous third-party books on Visual Basic for Applications (VBA). Microsoft Office applications provide online help for VBA. This is your primary reference for REALbasic office automation.

To access the online help, choose Macros from the Tools Menu of your MS Office application, and then choose Visual Basic Editor from the Macros submenu. When the Visual Basic editor appears, choose Microsoft Visual Basic Help from the Help menu. The help is contextual in the sense that it provides information on automating the Office application from which you launched the Visual Basic editor.

If VBA Help does not appear, you will need to install the VBA help files. On Windows Office 2003, Office prompts you to install the VBA help files when you first request VBA help. You don't need the master CD. On Macintosh, Office v.X does not install the VBA help files as part of the full install. Quit out of Office and locate your master CD. Open the "Value Pack" folder and double-click the Value Pack installer. In the Value Pack installer dialog, scroll down to the Programmability topic, select it, and click Continue. The installer will then add the VBA help files and examples to your Office installation. When the install finishes, the VBA help files will be available to the Visual Basic editor within all your Office X applications.

Microsoft has additional information on VBA at http://msdn.microsoft.com/vbasic/ and have published their own language references on VBA. One of several third-party books on VBA is "VB & VBA in a Nutshell: The Language" by Paul Lomax (ISBN: 1-56592-358-8).


Example

The following example transfers the information in a ListBox to Excel and tells Excel to compute and format a column total. The code is in a PushButton's Action event handler and it assumes that a two-column ListBox, ListBox1, is in the window. It contains the following data.

ItemPrice
Apples 5
Oranges 5
Bananas 5

Dim excel as ExcelApplication
Dim book as ExcelWorkbook
Dim sheet as ExcelWorksheet
Dim i as Integer
  
excel = New ExcelApplication
excel.Visible = True
book = excel.Workbooks.Add
excel.ActiveSheet.Name = "Expenses Report"
For i = 0 to Listbox1.listcount - 1
 excel.Range("A" + Str(i + 1), "A" + Str(i + 1)).value = listbox1.cell(i, 0)
next
excel.Range("A4", "A4").Value = "Total"
excel.Range("B1", "B3").Value = "5"
excel.Range("B1", "B3").Style = "Currency"
excel.Range("B4", "B4").Value = "=SUM(B1:B3)"
  
Exception err as OLEException
  MsgBox err.message

See Also

Office, OLEException, OLEObject, PowerPointApplication, WordApplication classes.